package com.xzt.meeting.dao;

import com.xzt.meeting.entity.Article;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;
import java.util.Map;

/**
 * @Author xzt
 * @Date 2022/5/8 18:12
 * @Version 1.0
 * @Introduce
 */
public class ArticleDao extends BaseDao{
    public List<Article> findPage(String title, Integer startIndex, Integer pageSize) {
        String sql = "select * from article where title like ? order by publish_date desc limit ?,?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Article.class),"%" + title + "%",startIndex,pageSize);

    }

    public Integer countBytitle(String title) {
        try {
            return jdbcTemplate.queryForObject("select count(*) from article where title like ?",Integer.class,"%" + title + "%");
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    public void save(Article article) {

        jdbcTemplate.update("insert into article values (null,?,?,?,?,?,?)",
                article.getTitle(),
                article.getContent(),
                article.getBrowseCount(),
                article.getPublishDate(),
                article.getPublishRealName(),
                article.getUserId());
    }

    public void updateBrowserCount(Long id) {
        jdbcTemplate.update("update article set browse_count=browse_count+1 where id=?",id);
    }

    public Article findById(Long id) {
        try{
            return jdbcTemplate.queryForObject("select * from article where id=?",new BeanPropertyRowMapper<>(Article.class),id);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    public Integer countFavoriteByArticleId(Long id) {
        try {
            return jdbcTemplate.queryForObject("select count(*) from favorite where a_id=?",Integer.class,id);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    public Integer countByUserIdAndArticleId(Long uid, Long aid) {
        try {
            return jdbcTemplate.queryForObject("select count(*) from favorite where u_id=? and a_id=?",Integer.class,uid,aid);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    public List<Map<String, Object>> findUserFavorite(Long uid, Long aid) {
        String sql = "SELECT u.id userId,u.real_name realName from userfocus uf " +
                "left join user u on u.id=uf.user_focus_id " +
                "left join favorite f on f.u_id=u.id " +
                "where uf.user_id=? and f.a_id=?";
        return jdbcTemplate.queryForList(sql,uid,aid);
    }

    public void unFavorite(Long uid, Long aid) {
        jdbcTemplate.update("delete from favorite where u_id=? and a_id=?",uid,aid);
    }

    public void favorite(Long uid, Long aid) {
        jdbcTemplate.update("insert into favorite values(?,?)",uid,aid);
    }

    public List<Article> favoritePage(Long id, String title, Integer startIndex, Integer pageSize) {
        String sql = "SELECT a.* from favorite f " +
                "left join article a on f.a_id=a.id " +
                "where f.u_id=? and a.title like ? " +
                "order by a.publish_date desc limit ?,?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Article.class),id,"%" + title + "%",startIndex,pageSize);
    }

    public Integer countFavoritePageBytitle(Long id, String title) {
        try {
            return jdbcTemplate.queryForObject("SELECT count(*) from favorite f left join article a on f.a_id=a.id where f.u_id=? and a.title like ?",Integer.class,id,"%" + title + "%");
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }
}
